在JSP 如何使用switch,
一個簡單的小範例,把0~9的數字變成ZERO ~ NINE
=== switch_sample.jsp ===
<HTML>
<HEAD>
<TITLE>use switch statement in jsp page</TITLE>
</HEAD>
<BODY bgcolor="#6E6E6E">
<FORM NAME="form1" ACTION="switch_sample.jsp" METHOD="get">
<TABLE bgcolor="#D8D8D8">
<tr>
<td> Enter number </td>
<td><input type="text" name="num"></td>
</tr>
<tr align="center"><td></td>
<td><INPUT TYPE="submit" VALUE="show"></td></tr></TABLE>
</FORM>
<br>
<TABLE bgcolor="#E0ECF8"><tr><td>
<%
if (request.getParameter("num") != null &&
request.getParameter("num") != "") {
for (int i = 0; i < request.getParameter("num").length(); i++) {
char value = request.getParameter("num").charAt(i);
// switch statement with some value
switch (value) {
case '0':
out.print("ZERO");
break;
case '1':
out.print("ONE");
break;
case '2':
out.println("TWO");
break;
case '3':
out.println("THREE");
break;
case '4':
out.println("FOUR");
break;
case '5':
out.println("FIVE");
break;
case '6':
out.println("SIX");
break;
case '7':
out.println("SEVEN");
break;
case '8':
out.println("EIGHT");
break;
case '9':
out.println("NINE");
break;
}
}
}
%>
</td></tr></TABLE>